<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<title></title>
		<script src="__INS__/js/plugins/socket.io/socket.io.js"></script>
		<script src="__INS__/js/jquery-2.1.1.js"></script>

		<script type="text/javascript">
	
			 function get_push() {
				$.getJSON("{:U('push/server')}", function(result) {
					if (result.status) {
						$content = eval('(' + result.data.data + ')');
						window.parent.push_info($content);
						 var options={
						            dir: "ltr",
						            lang: "utf-8",
						            icon: "/Uploads/notifylog.png",
						            body: $content.content,
						            title:$content.title,
						            url: $content.url
						        };
					
						shownotify(options);
						
					}
					
					/* if (result.status > 0) {												
						get_push();
					} */
				});
			}

			$(document).ready(function() {
				initNotify();
				get_push();
				// 连接服务端
				var socket = io('http://192.168.1.8:2120');
				// 连接后登录
				socket.on('connect', function() {
					socket.emit('login', '{:get_user_id()}');
				});
				// 后端推送来消息时
				socket.on('new_msg', function(msg) {
					
					get_push();
				});
			}); 
			
			
		function initNotify() {
				
				if (!("Notification" in window) && !window.webkitNotifications) {
					//不支持
					alert("当前浏览器不支持消息提醒！\n建议在谷歌、火狐等浏览器上使用此功能");
				} else if (Notification.permission != undefined && Notification.permission != null) {
					if (Notification.permission != "granted"){
						//无授权
			            window.Notification.requestPermission(function(status){
			                  if(Notification.permission !== status){
			                      Notification.permission = status;
			                  }
			              });
					} else{
						
					}
				} else if (Notification.permission == null || Notification.permission == undefined) {
					if (window.webkitNotifications.checkPermission() != 0)//未授权(360系列)
						$windowsNotify.attr("class", "close_click");
				} else {
				}

			}
			
			
			function shownotify(options){
				 var rand= Math.random()*100;
				 var n = new Notification(options.title, options);    
			     n.onshow = function(){
			         console.log("show ");
			     };
			     n.onclick = function() {
			    	 console.log(options.url);
			         window.open(options.url);
			     };
			     n.onclose = function(){
			         console.log("notification closed!");
			     };        
			     n.onerror = function() {
			         console.log("An error accured");
			     }          
				
			}
		</script>
	</head>
	<body></body>
</html>
